<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.1.0/dist/tf.min.js"></script>

  <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis"></script>
</head>

<body>


  <script>
    // console.log(tf.version)
    // console.log(tfvis)
    // const shape = [2, 3] // 2维2行3列
    // const a = tf.tensor([1, 2, 3, 4, 5, 6], shape)
    // a.print()
    // // 直接打印没东西的
    // console.log(a)


  </script>

  <script>

    class deepStudy {
      constructor() {
        this.model = null
        this.data = null
        this.dataReady()
        this.modelCreate()
        this.train()
        // this.infer()
        
      }
      // step1：数据预处理
      dataReady() {
        //   tf.util.shuffle(this.data);
        // 转换为张量 这里我们制作两个数组，一个用于我们的输入示例，另一个用于真正的输出值（在机器学习中称为标签）。

        let origin_data = [[800,10], [850,10], [900,10], [950,10], [980,10]];
        // let last_answers = [800000, 850000, 900000, 950000, 980000];
        let last_answers = [[800000,230], [850000,230], [900000,230], [950000,230], [980000,230]];
        this.length  = last_answers.length

        const xs = tf.data.array(origin_data);
        const ys = tf.data.array(last_answers);
        this.xyDataset = tf.data.zip({ xs: xs, ys: ys }).batch(this.length).shuffle(this.length);      
      }
      // step2：创造模型
      modelCreate() {
        // 创造一个 序贯模型(Sequential) 
        this.model = tf.sequential();

        // 输入空间的维数  输出空间的维数。
        this.model.add(tf.layers.dense({ inputShape: [2], units: 2 }));

        this.LEARNING_RATE = 0.01;
        this.EPOCHS = 50;
        this.BATCH_SIZE = 8;


      
      }
      // step3：开始训练
      async train() {
        this.model.compile({
          optimizer: tf.train.sgd(this.LEARNING_RATE),
          loss: 'meanAbsoluteError'
        });
        let results = await this.model.fitDataset(this.xyDataset,{
          epochs: this.EPOCHS,
          batchSize: this.BATCH_SIZE,
          shuffle: true,

        });

        console.log('training complete');
        this.infer()
      }
      // step4:开始推理
      infer() {
        // 推理的初始数据
        let predValue = [768,10];
        console.log("dsds")
        let answer = this.model.predict(tf.tensor2d([predValue]));
        answer.print();

        let nonTensorRepresentation = answer.dataSync();
        console.log("预测的y：",Math.floor(nonTensorRepresentation[0]),nonTensorRepresentation[1]);
       
        // 求预测的准确值
        // let result = this.model.evaluate(this.testTensors.data, this.testTensors.answer);
        // let testLoss = result.dataSync()[0];
        // console.log('Test loss: ' + testLoss);
      }
    }
    let model = new deepStudy()
    // console.log(model.modelCreate())

    // model.dataReady()
    // model.modelCreate()
    // model.train()
    // model.infer()







  </script>
</body>

</html>